//
//  PageTitleView.m
//  YuDou
//
//  Created by yingqi on 2018/6/22.
//  Copyright © 2018年 yingqi. All rights reserved.
//

#import "PageTitleView.h"

static CGFloat kScrollLineH = 4;
static CGFloat lineH = 0.5;
static CGFloat colorDeltaR = 170;
static CGFloat colorDeltaG = 43;
static CGFloat colorDeltaB = -85;

@interface PageTitleView()
{
    NSInteger _currentLabelIndex;
}
@property (nonatomic, strong) UIView *scrollLine;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSArray <NSString *>* titles;
@property (nonatomic, strong) NSMutableArray <UILabel *>* titleLabels;
//@property (nonatomic, assign) NSInteger currentLabelIndex;
@end

@implementation PageTitleView

- (NSMutableArray<UILabel *> *)titleLabels
{
    if (!_titleLabels) {
        _titleLabels = [NSMutableArray array];
    }
    return _titleLabels;
}

- (UIView *)scrollLine
{
    if (!_scrollLine) {
        _scrollLine = [[UIView alloc]init];
        _scrollLine.backgroundColor = [UIColor orangeColor];
    }
    return _scrollLine;
}

- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.scrollsToTop = NO;
        _scrollView.pagingEnabled = NO;
        _scrollView.bounces = NO;
        _scrollView.frame = self.bounds;
    }
    return _scrollView;
}

- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray <NSString *>*)titles
{
    self = [super initWithFrame:frame];
    if (self) {
        _currentLabelIndex = 0;
        self.titles = titles;
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    [self addSubview:self.scrollView];
    [self setupTitleLabels];
    [self setupBottomAndLin];
}


- (void)setupTitleLabels
{
    CGFloat labelW = self.width / self.titles.count;
    CGFloat labelH = self.height - kScrollLineH;
    CGFloat labelY = 0;
    
    for (int index = 0; index < self.titles.count; index++) {
        UILabel *label = [[UILabel alloc]init];
        label.text = self.titles[index];
        label.tag = index;
        label.font = [UIFont systemFontOfSize:15.0];
        label.textColor = kNormalColor;
        label.textAlignment = NSTextAlignmentCenter;
        
        CGFloat labelX = labelW * index;
        label.frame = CGRectMake(labelX, labelY, labelW, labelH);
        
        [self.scrollView addSubview:label];
        [self.titleLabels addObject:label];
        
        // 添加手势
        [label setUserInteractionEnabled:YES];
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(titleLabelClick:)];
        [label addGestureRecognizer:tapGes];
    }
}

- (void)setupBottomAndLin
{
    UIView *bottomLine = [[UIView alloc]init];
    bottomLine.backgroundColor = [UIColor lightGrayColor];
    bottomLine.frame = CGRectMake(0, self.height - lineH, self.width,  lineH);
    [self addSubview:bottomLine];
    
    UILabel *firstLabel = [self.titleLabels firstObject];
    firstLabel.textColor = kSelectColor;
    [self.scrollView addSubview:self.scrollLine];
    self.scrollLine.frame = CGRectMake(firstLabel.x+firstLabel.width*0.1,  self.height-kScrollLineH,  firstLabel.width*0.8,  kScrollLineH);
}

- (void)titleLabelClick:(UITapGestureRecognizer *)tapGes
{
    UILabel *currentLabel = (UILabel *)tapGes.view;
    if (currentLabel.tag == _currentLabelIndex) {return;}
    UILabel *oldLabel = self.titleLabels[_currentLabelIndex];
    currentLabel.textColor = kSelectColor;
    oldLabel.textColor = kNormalColor;
    _currentLabelIndex = currentLabel.tag;
    
    CGFloat scrollLinePosition = currentLabel.x + currentLabel.width * 0.1;
    self.scrollLine.width = currentLabel.width * 0.8;

    [UIView animateWithDuration:0.2 animations:^{
        self.scrollLine.x = scrollLinePosition;
    }];
    if (_delegate && [_delegate respondsToSelector:@selector(pageTitleView:selectedIndex:)]) {
        [_delegate pageTitleView:self selectedIndex:_currentLabelIndex];
    }
}


- (void)setTitleWithProgress:(CGFloat)progress sourceIndex:(NSInteger)sourceIndex targetIndex:(NSInteger)targetIndex
{
    UILabel *sourceLabel = self.titleLabels[sourceIndex];
    UILabel *targetLabel = self.titleLabels[targetIndex];
    CGFloat moveTotalX = targetLabel.x - sourceLabel.x;
    CGFloat moveX = moveTotalX * progress;
    self.scrollLine.x = sourceLabel.x + sourceLabel.width*0.1 + moveX;

    sourceLabel.textColor = kColor(255 - colorDeltaR * progress, 128 - colorDeltaG * progress,-colorDeltaB * progress);
    targetLabel.textColor = kColor(85 + colorDeltaR * progress, 85 + colorDeltaG * progress, 85 + colorDeltaB * progress);
    _currentLabelIndex = targetIndex;
}
@end

